package com.fpt.mockproject.j21.absd.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.SessionFactoryObserver;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

/**
 * HIBERNATE SIMPLIZER VERSION 1.2
 * This is a Abstract class used to start configure and initialize cfg.xml 
 * and create some object need to interactive with database throught Hibernate API
 * 
 * Hibernate Version required : 4.3.4
 * 
 * @author Linh HB Breaker
 * @version 1.2
 * @since 27/06/2014
 */
public abstract class HibernateSimplizerCofigAndInit{
	
	protected SessionFactory sessionFactory;

	protected ServiceRegistry standardServiceRegistry;
	
	protected Class<?> annotatedClass;
	
	protected Session session;
	
	public Class<?> getAnnotatedClass() {
		return annotatedClass;
	}

	public void setAnnotatedClass(Class<?> annotatedClass) {
		this.annotatedClass = annotatedClass;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public ServiceRegistry getStandardServiceRegistry() {
		return standardServiceRegistry;
	}
	
	public Session getSession() {
		return session;
	}
	
	/**
	 * Starting config with annotated Class injected before transaction start.
	 * @param annotatedClass
	 */
	public HibernateSimplizerCofigAndInit(Class<?> annotatedClass) {
		this.annotatedClass = annotatedClass;
		Configuration configuration = new Configuration();
	    configuration.configure().addAnnotatedClass(this.annotatedClass);
	    configuration.setSessionFactoryObserver(new SessionFactoryObserver() {
			
			@Override
			public void sessionFactoryCreated(SessionFactory arg0) {
			}
			
			@Override
			public void sessionFactoryClosed(SessionFactory arg0) {
				StandardServiceRegistryBuilder.destroy(standardServiceRegistry);
			}
		});
		this.standardServiceRegistry = new StandardServiceRegistryBuilder().applySettings(
	            configuration.getProperties()).build();
	    this.sessionFactory = configuration.buildSessionFactory(standardServiceRegistry);
	    this.session = this.sessionFactory.openSession();
	}
	
	/**
	 * Starting config with NO annotated class
	 * @param annotatedClass
	 */
	public HibernateSimplizerCofigAndInit() {
		Configuration configuration = new Configuration();
	    configuration.configure();
	    configuration.setSessionFactoryObserver(new SessionFactoryObserver() {
			
			@Override
			public void sessionFactoryCreated(SessionFactory arg0) {
			}
			
			@Override
			public void sessionFactoryClosed(SessionFactory arg0) {
				StandardServiceRegistryBuilder.destroy(standardServiceRegistry);
			}
		});
		this.standardServiceRegistry = new StandardServiceRegistryBuilder().applySettings(
	            configuration.getProperties()).build();
	    this.sessionFactory = configuration.buildSessionFactory(standardServiceRegistry);
	    this.sessionFactory = configuration.buildSessionFactory(standardServiceRegistry);
	    this.session = this.sessionFactory.openSession();
	}
	
	/**
	 * close, terminate hibernate object
	 */
	public void close()
	{
		this.session.close();
		this.sessionFactory.close();
	}
}
